﻿using DataTransfer.Core.Configurations;
using DataTransfer.Core.Domains;
using DataTransfer.SDK.Models;
using MiniFox.Components;
using MiniFox.Configurations;

namespace DataTransfer.Core.Providers
{
    /// <summary>
    /// 
    /// </summary>
    [AutoRegister, SpareComponent(typeof(ExportConfigProvider))]
    public interface IExportConfigProvider
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        IExporter GetExporter(string name);
    }

    internal class ExportConfigProvider : IExportConfigProvider
    {
        public IExporter GetExporter(string name)
        {
            var settings = ConfigurationFactory.GetConfig<ExcelExportSettingCollection>();
            var setting = settings[name];
            return new ExcelExporter
            {
                Name = setting.Name,
                DataLoaderTypeName = setting.DataLoaderTypeName,
                ExcelFormat = Enum.TryParse(setting.ExcelFormat, true, out ExcelFormat excelFormat)? excelFormat : ExcelFormat.XLS,
                MaxRowCountPerSheet = setting.MaxRowCountPerSheet,
                //Template = setting.Template,
                Modules = new Dictionary<string, Type>(setting.Modules.Select(p => new KeyValuePair<string, Type>(p.Name, p))),
                Sheets = setting.Sheets.Select(s => new ExcelExportSheet
                {
                    SheetName = s.SheetName,
                    Bordered = s.Bordered,
                    HeaderHeight = s.HeaderHeight,
                    HeaderRowIndex = s.HeaderRowIndex,
                    QueryName = s.QueryName,
                    RowHeight = s.RowHeight,
                    SkipColumnCount = s.SkipColumnCount,
                    SkipRowCount = s.SkipRowCount,
                    Fields = s.Fields.Select(f => new ExcelExportSheetField
                    {
                        FieldName = f.FieldName,
                        DisplayName = f.DisplayName,
                        FontSize = f.FontSize,
                        Format = f.Format,
                        HeaderFontSize = f.HeaderFontSize,
                        Width = f.Width,
                        TypeName = f.FieldType
                    }).ToArray()
                })
            };
        }
    }
}
